/**
 * @Description: 自动发布（方式本地测试账号地址信息被发布）
 * @author Beon
 * @date 2021/1/14
*/
/* eslint-disable */
const childProcess = require("child_process");
const fs = require('fs');
const p = require('path');
const ora = require("ora");

// 执行发布
(async () => {
    const loading = ora();
    try {
        let path = p.join(__dirname, './dist');
        deleteFolder(path);

        loading.start('开始构建')
        await childProcess.execSync('tsc')
        loading.succeed('构建成功')

        loading.start('开始发布')
        await childProcess.execSync('npm publish')
        loading.succeed('发布成功')
    } catch (e) {

    }
})()

/**
 * 根据路径删除文件夹
 * @param {String} path 路径
 */
function deleteFolder(path) {
    let files = [];
    if (fs.existsSync(path)) {
        files = fs.readdirSync(path);
        files.forEach(function(file, index){
            let curPath = path + '/' + file;
            if (fs.statSync(curPath).isDirectory()) {
                deleteFolder(curPath);
            } else {
                fs.unlinkSync(curPath);
            }
        });
        fs.rmdirSync(path);
    }
}

